<?php 
class Poll_Installer extends Engine_Package_Installer_Module
{
    public function onInstall() { 
        $db     = $this->getDb();
        $select = new Zend_Db_Select($db);

        $select
        ->from('engine4_core_pages')
        ->where('name = ?', 'user_profile_index')
        ->limit(1);
        $page_id = $select->query()->fetchObject()->page_id;

        $select = new Zend_Db_Select($db);
        $select
        ->from('engine4_core_content')
        ->where('page_id = ?', $page_id)
        ->where('type = ?', 'widget')
        ->where('name = ?', 'poll.profile-questions')
        ;

        $info = $select->query()->fetch();

        if( empty($info) ) {  
            $select = new Zend_Db_Select($db);
            $select
            ->from('engine4_core_content')
            ->where('page_id = ?', $page_id)
            ->where('type = ?', 'container')
            ->limit(1);
            $container_id = $select->query()->fetchObject()->content_id;
 
            $select = new Zend_Db_Select($db);
            $select
            ->from('engine4_core_content')
            ->where('parent_content_id = ?', $container_id)
            ->where('type = ?', 'container')
            ->where('name = ?', 'middle')
            ->limit(1);
            $middle_id = $select->query()->fetchObject()->content_id;

            $select
            ->reset('where')
            ->where('type = ?', 'widget')
            ->where('name = ?', 'core.container-tabs')
            ->where('page_id = ?', $page_id)
            ->limit(1);
            $tab_id = $select->query()->fetchObject();
            if( $tab_id && @$tab_id->content_id ) {
                $tab_id = $tab_id->content_id;
            } else {
                $tab_id = null;
            }
            
            $db->insert('engine4_core_content', array(
            'page_id' => $page_id,
            'type'    => 'widget',
            'name'    => 'poll.profile-questions',
            'parent_content_id' => ($tab_id ? $tab_id : $middle_id),
            'order'   => 4,
            'params'  => '{"title":"Questions","titleCount":true}',
            ));

        }
        
        $select = new Zend_Db_Select($db);
        $select
        ->from('engine4_core_pages')
        ->where('name = ?', 'poll_poll_browse')
        ->limit(1);
        $page_id = $select->query()->fetchObject()->page_id;
        if (!$page_id) {
            $db->insert('engine4_core_pages', array(
                'name' => 'poll_poll_browse',
                'displayname' => 'Browse Questions',
                'title' => 'Browse Questions',
                'description' => 'This is Browse Questions Poll Page.',
                'custom' => 1,
            ));
            $page_id = $db->lastInsertId('engine4_core_pages');
            
            $db->insert('engine4_core_content', array(
                'page_id' => $page_id,
                'type' => 'container',
                'name' => 'top',
                'parent_content_id' => null,
                'order' => 1,
                'params' => '',
            ));
            $top_id = $db->lastInsertId('engine4_core_content'); 
            $db->insert('engine4_core_content', array(
                'page_id' => $page_id,
                'type' => 'container',
                'name' => 'middle',
                'parent_content_id' => $top_id,
                'order' => 2,
                'params' => '',
            ));
            $top_middle_id = $db->lastInsertId('engine4_core_content');            
            $db->insert('engine4_core_content', array(
                'page_id' => $page_id,
                'type' => 'widget',
                'name' => 'poll.navigator',
                'parent_content_id' => $top_middle_id,
                'order' => 3,
                'params' => '',
            )); 
            
            $db->insert('engine4_core_content', array(
                'page_id' => $page_id,
                'type' => 'container',
                'name' => 'main',
                'parent_content_id' => null,
                'order' => 2,
                'params' => '',
            ));
            $container_id = $db->lastInsertId('engine4_core_content');
            
            $db->insert('engine4_core_content', array(
                'page_id' => $page_id,
                'type' => 'container',
                'name' => 'left',
                'parent_content_id' => $container_id,
                'order' => 2,
                'params' => '',
            ));
            $left_id = $db->lastInsertId('engine4_core_content');
            
            $db->insert('engine4_core_content', array(
                'page_id' => $page_id,
                'type' => 'container',
                'name' => 'right',
                'parent_content_id' => $container_id,
                'order' => 3,
                'params' => '',
            ));
            $right_id = $db->lastInsertId('engine4_core_content');
            
            $db->insert('engine4_core_content', array(
                'page_id' => $page_id,
                'type' => 'container',
                'name' => 'middle',
                'parent_content_id' => $container_id,
                'order' => 4,
                'params' => '',
            ));
            $middle_id = $db->lastInsertId('engine4_core_content');
            
            $db->insert('engine4_core_content', array(
                'page_id' => $page_id,
                'type' => 'widget',
                'name' => 'poll.search-form',
                'parent_content_id' => $left_id,
                'order' => 2,
                'params' => '',
            ));
            
            $db->insert('engine4_core_content', array(
                'page_id' => $page_id,
                'type' => 'widget',
                'name' => 'poll.browse',
                'parent_content_id' => $middle_id,
                'order' => 2,
                'params' => '',
            ));
            
            $db->insert('engine4_core_content', array(
                'page_id' => $page_id,
                'type' => 'widget',
                'name' => 'poll.friends-questions',
                'parent_content_id' => $right_id,
                'order' => 2,
                'params' => '{"title":"Friends Questions"}',
            )); 
            
            $db->insert('engine4_core_content', array(
                'page_id' => $page_id,
                'type' => 'widget',
                'name' => 'poll.latest-questions',
                'parent_content_id' => $right_id,
                'order' => 3,
                'params' => '{"title":"Latest Questions"}',
            ));
            
            $db->insert('engine4_core_content', array(
                'page_id' => $page_id,
                'type' => 'widget',
                'name' => 'poll.popular-questions',
                'parent_content_id' => $right_id,
                'order' => 4,
                'params' => '{"title":"Popular Questions"}',
            ));          
        }
        
        parent::onInstall();
    }
}